# -*- coding:utf-8 -*-

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.views.generic.simple import direct_to_template
from django.core.urlresolvers import reverse
from tsuchinoco.accounts.models import Accounts

def login(request):
    """
    """
    if request.method == 'GET':
        request.session['user_id'] = None
        return direct_to_template(request, 'login.html', { 'foo': '','message':'' })
    elif request.method == 'POST':
        id = request.POST["name"]
        pwd = request.POST["password"]
        exists = Accounts.objects.filter(account_name=id, account_password=pwd).count()
        if exists == 1:
          accnt = Accounts.objects.get(account_name=id, account_password=pwd)
          print accnt.id
          request.session['user_id'] = accnt.id
          request.session.set_expiry(0)
          return HttpResponseRedirect('./portal/%d/'%accnt.id)
        else:
          return  direct_to_template(request, 'login.html', {'foo':'', 'message':''})

